<?php


namespace app\backend\controller\v1;

use app\backend\validate\BannerValidate;
use app\common\model\Banner as BannerModel;
use think\exception\ValidateException;
use think\facade\Filesystem;

class Banner extends BaseController
{
    /**
     * 轮播列表
     *
     * @return \think\Response
     */
    public function index()
    {
        // 轮播表
        $user = new BannerModel();
        // where条件
        $where = [];
        $status = input('get.status/d', 1);
        if (!empty($status)) {
            $where[] = ['status', '=', $status];
        }
        // 分页、排序等
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 15);
        $sort = ['id' => 'desc', 'status' => 'desc'];
        $field = 'id,image,url,type';
        //获取列表
        $list = $user->fetchListByWhere($where, $sort, $field, $page, $pageSize);
        return sucessMsg(200, '轮播图获取成功！', ['list' => $list, 'total' => count($list)]);
    }

    /**
     * 显示创建轮播表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $user = new BannerModel();
        $data = input();
        $img = $file = request()->file('image');
        try {
            validate(BannerValidate::class)->scene('create')->check($data);
            if (empty($img)) {
                return errMsg(422, '请上传图片呀!');
            }
            $saveName = Filesystem::disk('public')->putFile('/uploads', $file);
            $data['image'] = $saveName;
            if ($user->save($data)) {
                return sucessMsg(200, '创建成功！', $user->id);
            }
            return errMsg(500, '创建失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }


    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg(400, '请选择轮播图！');
        }
        $user = BannerModel::find($id);
        $data = input();
        $img = $file = request()->file('image');
        try {
            validate(BannerValidate::class)->scene('update')->check($data);
            if (!empty($img)) {
                $saveName = Filesystem::disk('public')->putFile('/uploads', $file);
                $data['image'] = $saveName;
            }
            if (BannerModel::update($data, $id)) {
                return sucessMsg(200, '修改成功！', $user->id);
            }
            return errMsg(500, '修改失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            return errMsg(400, '请选择要删除用户id！');
        }
        if (BannerModel::destroy($id)) {
            return sucessMsg(200, '删除成功！');
        }
        return errMsg(500, '删除失败！');
    }
}